package com.shop.front.controller;

import com.shop.common.aop.NotLogin;
import com.shop.common.core.AjaxResult;
import com.shop.common.core.PageResult;
import com.shop.front.service.ICourseService;
import com.shop.front.validate.common.IdValidate;
import com.shop.front.validate.common.PageValidate;
import com.shop.front.validate.course.CourseReportValidate;
import com.shop.front.validate.course.CourseSearchValidate;
import com.shop.front.vo.course.CourseCategoryListedVo;
import com.shop.front.vo.course.CourseDetailVo;
import com.shop.front.vo.course.CourseListedVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/api/course")
@Api(tags = "课程管理")
public class CourseController {
    @Resource
    ICourseService iCourseService;

    /**
     * 课程分类
     */
    @GetMapping("/category")
    @ApiOperation(value="课程分类")
    @NotLogin
    public AjaxResult<List<CourseCategoryListedVo>> category() {
        List<CourseCategoryListedVo> list = iCourseService.category();
        return AjaxResult.success(list);
    }

    /**
     * 分类详情
     */
    @GetMapping("/categoryDetail")
    @ApiOperation(value="分类详情")
    @NotLogin
    public AjaxResult<CourseCategoryListedVo> categoryDetail(Integer id) {
        return AjaxResult.success(iCourseService.category(id));
    }

    /**
     * 获取精选课程
     */
    @GetMapping("/selected")
    @ApiOperation(value="精选课程")
    @NotLogin
    public AjaxResult<List<CourseListedVo>> selected() {
        List<CourseListedVo> list = iCourseService.selected();
        return AjaxResult.success(list);
    }

    /**
     * 获取课程列表
     */
    @GetMapping("/list")
    @ApiOperation(value="课程列表")
    @NotLogin
    public AjaxResult<PageResult<CourseListedVo>> list(@Validated CourseSearchValidate searchValidate, @Validated PageValidate pageValidate) {
        PageResult<CourseListedVo> list = iCourseService.list(searchValidate, pageValidate);
        return AjaxResult.success(list);
    }

    /**
     * 课程详情
     */
    @GetMapping("/detail")
    @ApiOperation(value="课程详情")
    public AjaxResult<CourseDetailVo> detail(Integer id) {
        return AjaxResult.success(iCourseService.detail(id));
    }

    /**
     * 上报学习状态（更新记录学习时间）
     */
    @PostMapping("/report")
    @ApiOperation(value="上报学习状态")
    public AjaxResult<Object> report(@Validated @RequestBody CourseReportValidate courseReportValidate) {
        iCourseService.report(courseReportValidate.getCourseId(), courseReportValidate.getHistoryId());
        return AjaxResult.success();
    }

    /**
     * 收藏课程
     */
    @PostMapping("/collect")
    @ApiOperation(value="收藏课程")
    public AjaxResult<Object> collect(@Validated @RequestBody IdValidate idValidate) {
        iCourseService.collect(idValidate.getId());
        return AjaxResult.success();
    }

    /**
     * 取消收藏课程
     */
    @PostMapping("/cancelCollect")
    @ApiOperation(value="取消收藏课程")
    public AjaxResult<Object> cancelCollect(@Validated @RequestBody IdValidate idValidate) {
        iCourseService.cancelCollect(idValidate.getId());
        return AjaxResult.success();
    }

    /**
     * 获取收藏课程分页
     */
    @GetMapping("/collected")
    @ApiOperation(value="收藏课程分页")
    public AjaxResult<PageResult<CourseListedVo>> collected(@Validated PageValidate pageValidate) {
        PageResult<CourseListedVo> list = iCourseService.collected(pageValidate);
        return AjaxResult.success(list);
    }

    /**
     * 获取课程学习记录
     */
    @GetMapping("/history")
    @ApiOperation(value="课程学习记录")
    public AjaxResult<PageResult<CourseListedVo>> history(@Validated PageValidate pageValidate) {
        PageResult<CourseListedVo> list = iCourseService.history(pageValidate);
        return AjaxResult.success(list);
    }

    /**
     * 清空历史记录
     */
    @PostMapping("/clearHistory")
    @ApiOperation(value="清空历史记录")
    public AjaxResult<Object> clearHistory() {
        iCourseService.clearHistory();
        return AjaxResult.success();
    }
}
